layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoSpaceUser = {
        id: 'BoSpaceUserTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'account', title: '帐号', align: 'center'},
            {field: 'userName', title: '用户名', align: 'center'},
            {field: 'headUrl', title: '头像', align: 'center',
                templet: function (object) {
                    return '<img src="/head_photo/' + object.headUrl + '" style="width: 40px; height: 40px;border-radius: 50%;">';
                }
            },
            {field: 'sex', title: '性别', align: 'center', width: 70,
                templet: function (object) {
                    if("0" == object.sex){
                        return '<span class="layui-badge layui-bg-blue">男</span>';
                    }else if("1" == object.sex){
                        return '<span class="layui-badge">女</span>';
                    }
                }
            },
            {field: 'type', title: '类型', align: 'center', width: 75,
                templet: function (object) {
                    if("0" == object.type){
                        return '<span class="layui-badge layui-bg-blue">他</span>';
                    }else if("1" == object.type){
                        return '<span class="layui-badge">她</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-orange">其他</span>';
                    }
                }
            },
            {field: 'age', title: '年龄', align: 'center',  width: 50},
            {field: 'bothdayValue', title: '生日', align: 'center',
                templet: function (object) {
                    if("" == object.bothdayValue || undefined == object.bothdayValue){
                        return '-';
                    }else{
                        return object.bothdayValue;
                    }
                }
            },
            {field: 'tall', title: '身高(cm)', align: 'center', width: 75,
                templet: function (object) {
                    if("" == object.tall || undefined == object.tall){
                        return '-';
                    }else{
                        return object.tall + 'cm';
                    }
                }
            },
            {field: 'phone', title: '手机号', align: 'center',
                templet: function (object) {
                    if("" == object.phone || undefined == object.phone){
                        return '-';
                    }else{
                        return object.phone;
                    }
                }
            },
            {field: 'professional', title: '职业', align: 'center',
                templet: function (object) {
                    if("" == object.professional || undefined == object.professional){
                        return '-';
                    }else{
                        return object.professional;
                    }
                }
            },
            {field: 'hobby', title: '爱好', align: 'center',
                templet: function (object) {
                    if("" == object.hobby || undefined == object.hobby){
                        return '-';
                    }else{
                        return object.hobby;
                    }
                }
            },
            {field: 'introduce', title: '介绍', align: 'center',
                templet: function (object) {
                    if("" == object.introduce || undefined == object.introduce){
                        return '-';
                    }else{
                        return object.introduce;
                    }
                }
            },
            {field: 'remark', title: '备注', align: 'center',
                templet: function (object) {
                    if("" == object.remark || undefined == object.remark){
                        return '-';
                    }else{
                        return object.remark;
                    }
                }
            },
            {field: 'status', title: '帐号状态', align: 'center', width: 75,
                templet: function (object) {
                    if("0" == object.status){
                        return '<span class="layui-badge layui-bg-blue">正常</span>';
                    }else if("1" == object.status){
                        return '<span class="layui-badge">冻结</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-orange">封停</span>';
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', minWidth: 230, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoSpaceUser.id,
        url: '/web/boSpaceUser/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var userName = $("#search_userName").val();
        var sex = $("#search_sex").val();
        var type = $("#search_type").val();
        var status = $("#search_status").val();

        //封装
        var dataObject = {};
        dataObject['userName'] = userName;
        dataObject['sex'] = sex;
        dataObject['type'] = type;
        dataObject['status'] = status;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoSpaceUser.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_userName").val("");
        $("#search_sex").val("-1");
        $("#search_type").val("-1");
        $("#search_status").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['userName'] = "";
        dataObject['sex'] = "";
        dataObject['type'] = "";
        dataObject['status'] = "";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoSpaceUser.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听添加操作
    $(".layuimini-container").on("click", "#add-btn", function () {
        var content = layuimini.getHrefContent('/web/boSpaceUser/add');
        var index = layer.open({
            title: '添加用户',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['800px','700px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoSpaceUser.id);
            }
        });

        return true;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoSpaceUser.onEdit = function(id){
        var content = layuimini.getHrefContent('/web/boSpaceUser/edit?id=' + id);
        var index = layer.open({
            title: '编辑用户',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['800px','700px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoSpaceUser.id);
            }
        });

        return false;
    };

    /**
     * 点击修改密码按钮
     * @param id
     */
    BoSpaceUser.onPassword = function(id){
        var content = layuimini.getHrefContent('/web/boSpaceUser/onPassword?id=' + id);
        var index = layer.open({
            title: '修改密码',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['400px','250px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoSpaceUser.id);
            }
        });

        return false;
    };

    /**
     * 点击修改帐号状态按钮
     * @param id
     */
    BoSpaceUser.onStatus = function(id){
        var content = layuimini.getHrefContent('/web/boSpaceUser/onStatus?id=' + id);
        var index = layer.open({
            title: '修改帐号状态',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['400px','300px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoSpaceUser.id);
            }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    BoSpaceUser.onDelete = function(id){
        layer.open({
            title: '删除用户'
            ,content: '是否确定删除该用户？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boSpaceUser/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoSpaceUser.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoSpaceUser.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'delete') {
            BoSpaceUser.onDelete(data.id);
        } else if (layEvent === 'edit') {
            BoSpaceUser.onEdit(data.id)
        } else if (layEvent === 'password') {
            BoSpaceUser.onPassword(data.id)
        } else if (layEvent === 'status') {
            BoSpaceUser.onStatus(data.id)
        }
    });

});